/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-09-01 09:15:51
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-11-13 14:18:04
 * @FilePath: \cxl-admin\src\pages\finance\reconciliationCollect\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import * as tableConfig from './config.json'
import { CreateXMLHttpRequest } from '@/utils'
import {
    paramsReconciliationInterface,
    reconciliationDetailedList,
    reconciliationList,
    reconciliationTotal
} from '@/apis/finance/reconciliation'
import { ElMessage } from 'element-plus'
import { reactive } from 'vue'

export class ReconciliationCollectData extends BaseData implements BaseMethod {
    //选择的时间段
    date: string[] = []
    companyId = ''
    detailed = reactive({
        visible: false,
        title: 'xxx公司',
        customerNo: '',
        fromTo: [''],
        companyId: ''
    })
    constructor() {
        super(tableConfig)
        this.pageData.search.unshift(
            ...[
                {
                    key: 'date',
                    type: 'daterange',
                    name: '',
                    modelValue: this.fromTo,
                    placeholder: '请选择时间',
                    change: (res: any) => {
                        this.date = res
                    }
                }
            ]
        )
        this.date.length = 0
        this.date.push(...this.fromTo)

        if (this.uStore.isSuperAdmin || Number(this.uStore.companyId) === 1) {
            //超级管理员
            // this.searchList.push({ key: 'queryContent', type: 'text', name: '客户编号|名称' })
            this.pageData.search.push([''])
            this.pageData.table.summaryType = 'reconciliationCollectSuperAdmin'
        } else {
            //企业管理员
            this.operation.length = 0
            this.propName = tableConfig.table.companyPropName
            this.createTablePropName()
            this.pageData.table.summaryType = 'reconciliationDetailed'
        }

        this.getData()
    }

    onOperation = (arg?: any) => {
        const { type, data } = arg
        if (type === 'export') {
            if (this.uStore.isSuperAdmin || Number(this.uStore.companyId) === 1) {
                const params = `current=99&size=99&companyId=${''}&queryDateStart=${this.date[0] || ''}&queryDateEnd=${
                    this.date[1] || ''
                }`
                console.log('params', params)
                const url =
                    import.meta.env.VITE_HOST + '/api/mall/company/dayStatistics/checkAccountPageListExport?' + params

                CreateXMLHttpRequest(url, '对账汇总列表')
            } else {
                const params = `current=99&size=99&companyId=${this.uStore.companyId}&queryDateStart=${
                    this.date[0] || ''
                }&queryDateEnd=${this.date[1] || ''}`
                console.log('params', params)
                const url =
                    import.meta.env.VITE_HOST +
                    '/api/mall/company/dayStatistics/checkAccountDetailPageListExport?' +
                    params

                CreateXMLHttpRequest(url, this.uStore.company + '对账汇总明细列表')
            }
        } else if (type === 'look') {
            this.detailed.visible = true
            this.detailed.title = data.companyName
            this.detailed.fromTo = this.date
            this.detailed.customerNo = data.customerNo
            this.detailed.companyId = data.companyId
        }
    }

    /**
     * 监听点击搜索按钮
     * @param res
     */
    onSearch = (res: any) => {
        this.date = res.date
        this.companyId = res.companyId
        this.getData()
    }

    /**
     * 监听点击搜索重置按钮
     */
    onReset = () => {
        this.getData()
    }

    /**
     * 监听分页发生改变
     * @param index
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }

    /**
     * 监听选择每页显示个数
     * @param size
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }

    /**
     * 拉取数据
     */
    getData() {
        const params: paramsReconciliationInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize,
            queryDateStart: this.date[0],
            queryDateEnd: this.date[1]
        }

        if (this.companyId) {
            params.companyId = this.companyId
        }
        if (this.uStore.isSuperAdmin || Number(this.uStore.companyId) === 1) {
            //超级管理员
            reconciliationList(params).then(res => {
                if (res.data.value === 200) {
                    this.pageData.table.data.length = 0
                    res.data.data.records.forEach((item: any) => {
                        item.primaryRemainStoreAmount = item.primaryRemainStoreAmount || '0.00'
                        item.storeAmount = item.storeAmount || '0.00'
                        item.realTimeStoreAmount = item.realTimeStoreAmount || '0.00'
                        item.maintenanceSearchAmount = item.maintenanceSearchAmount || '0.00'
                        item.crashSearchAmount = item.crashSearchAmount || '0.00'
                        item.powerBatterySearchAmount = item.powerBatterySearchAmount || '0.00'
                        item.refundAmount = item.refundAmount || '0.00'
                        item.endRemainStoreAmount = item.endRemainStoreAmount || '0.00'
                        this.pageData.table.data.push(item)
                    })

                    this.pageData.pagination.total = res.data.data.total
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
            reconciliationTotal(params).then(res => {
                if (res.data.value === 200) {
                    if (res.data.data) {
                        this.pageData.table.summaryArray = [
                            '合计', // 合计占2列
                            '', //本期期初余额
                            res.data.data.storeAmountSum || '0.00', //本期充值金额
                            res.data.data.realTimeStoreAmountSum || '0.00', //实时充值金额
                            res.data.data.crashSearchAmountSum || '0.00', //本期出险车况报告消费
                            res.data.data.maintenanceSearchAmountSum || '0.00', //本期维保车况报告消费
                            res.data.data.powerBatterySearchAmount || '0.00', //本期新能源车况报告消费
                            res.data.data.refundAmountSum || '0.00', //本期退款金额
                            '', //期末可用余额
                            '' //操作
                        ]
                        console.log(this.pageData.table.summaryArray)
                    }
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
        } else {
            //企业管理员
            params.companyId = this.uStore.companyId
            reconciliationDetailedList(params).then(res => {
                if (res.data.value === 200) {
                    this.pageData.table.data.length = 0
                    if (res.data.data.records.length) {
                        this.fromTo[0] = res.data.data.records[0].dataDate
                        this.pageData.table.data.push({
                            dataDate: res.data.data.records[0].dataDate,
                            storeAmount: '-',
                            realTimeStoreAmount: '-',
                            maintenanceSearchAmount: '-',
                            crashSearchAmount: '-',
                            powerBatterySearchAmount: '-',
                            refundAmount: '-',
                            endRemainStoreAmount: res.data.data.records[0].primaryRemainStoreAmount || '0.00'
                        })
                    }

                    this.pageData.table.data.push(...res.data.data.records)
                    this.pageData.pagination.total = res.data.data.total
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
            reconciliationTotal(params).then(res => {
                console.log(res.data.data)
                if (res.data.value === 200) {
                    if (res.data.data) {
                        this.pageData.table.summaryArray = [
                            '小计', //占2列
                            '', //预付充值
                            res.data.data.realTimeStoreAmountSum || '0.00', //实时充值
                            res.data.data.maintenanceSearchAmountSum || '0.00', //维保车况
                            res.data.data.crashSearchAmountSum || '0.00', //出险车况
                            res.data.data.powerBatterySearchAmount || '0.00', //新能源车况
                            res.data.data.refundAmountSum || '0.00', //退费
                            res.data.data.storeAmountSum || '0.00' //充值余额
                        ]
                    }
                } else {
                    ElMessage.error(res.data.desc)
                }
            })
        }
    }
    //监听弹窗隐藏
    onReconciliationCollectDetailedVisible = () => {
        this.detailed.visible = false
    }
}
